package com.bridgeintelligent.tag.webserver.dim.mgmt.service.index;

import com.bridgeintelligent.tag.constants.TagConstants;
import com.bridgeintelligent.tag.mapper.CatalogMapper;
import com.bridgeintelligent.tag.mapper.IndexMapper;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.CustJiance;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.DimCatalog;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.DimData;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.enums.IndexBaseEnum;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.vo.CustomersDownloadVO;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.vo.IndexDimCatalogVO;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.vo.IndexMonitorVO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @description
 * @author: grp 2021-01-06 10:38
 **/
@Service
@AllArgsConstructor
@Slf4j
public class IndexServiceImpl implements IndexService{

    private IndexMapper indexMapper;
    private CatalogMapper catalogMapper;

    @Override
    public List<IndexDimCatalogVO> dimCatalog() {
        List<IndexDimCatalogVO> list = indexMapper.dimCatalog();
        if(!CollectionUtils.isEmpty(list)){
            return list.stream().filter(o->!StringUtils.equals(TagConstants.PRIVATE_CATALOGID,o.getName()))
                    .collect(Collectors.toList());
        }
        return list;
    }

    @Override
    public Map<String, List<IndexDimCatalogVO>> dimUseNum() {
        Map<String, List<IndexDimCatalogVO>> map = new HashMap<>();
        List<IndexDimCatalogVO> list = indexMapper.dimCatalog();
        List<IndexDimCatalogVO> dimCatalogVOList =
                list.stream().filter(o->!StringUtils.equals(TagConstants.PRIVATE_CATALOGID,o.getName()))
                .collect(Collectors.toList());
        for (IndexDimCatalogVO indexDimCatalogVO : dimCatalogVOList) {
            //该目录下所有的标签
            List<DimData> dimList = new ArrayList<>();
            //获取该目录下的所有子目录
            List<DimCatalog> dimCatalogs = catalogMapper.findNext(indexDimCatalogVO.getId());
            List<DimCatalog> all = new ArrayList<>(dimCatalogs);
            all.add(new DimCatalog().setId(indexDimCatalogVO.getId()));
            //查找该目录下的所有标签
            for (DimCatalog catalog : dimCatalogs) {
                all.addAll(catalogMapper.findNext(catalog.getId()));
            }
            if(CollectionUtils.isNotEmpty(all)){
                dimList.addAll(indexMapper.findDimsByCatalogIds(all));
            }
            //标签id
            List<String> dimIdList = dimList.stream().map(DimData::getDimId).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(dimIdList)){
                map.put(indexDimCatalogVO.getId(),new ArrayList<>());
                continue;
            }
            Map<String,Object> param = new HashMap<>();
            param.put("items",dimIdList);
            //查询前20个标签的数量
            List<IndexDimCatalogVO> dimCatalogVOS = indexMapper.dimUseNum(param);
            List<String> idStr = dimCatalogVOS.stream().map(IndexDimCatalogVO::getId).collect(Collectors.toList());
            for (DimData dimData : dimList) {
                if(dimCatalogVOS.size()>=20){
                    break;
                }
                if (!idStr.contains(dimData.getDimId())){
                    IndexDimCatalogVO indexDimCatalogVO1 = new IndexDimCatalogVO();
                    indexDimCatalogVO1.setId(dimData.getDimId());
                    indexDimCatalogVO1.setName(dimData.getDimName());
                    indexDimCatalogVO1.setBusinessLeader(dimData.getBusinessLeader());
                    indexDimCatalogVO1.setNum("0");
                    indexDimCatalogVO1.setNo(String.valueOf(dimCatalogVOS.size()+1));
                    indexDimCatalogVO1.setFirst(dimData.getFirst());
                    indexDimCatalogVO1.setSecond(dimData.getSecond());
                    dimCatalogVOS.add(indexDimCatalogVO1);
                }
            }
            map.put(indexDimCatalogVO.getId(),dimCatalogVOS);
        }
        return map;
    }

    @Override
    public List<CustomersDownloadVO> customersDownloadNum() {
        return indexMapper.countDownloads();
    }

    @Override
    public Map<String,IndexMonitorVO> monitorNum() {
        Map<String,IndexMonitorVO> map = new HashMap<>();
        for(IndexBaseEnum base:IndexBaseEnum.values()){
            map.put(base.getName(),getValue(base));
        }
        return map;
    }

    private IndexMonitorVO getValue(IndexBaseEnum base) {
        IndexMonitorVO indexMonitorVO = indexMapper.findHistoryData(base.getHistoryFlag());
        CustJiance custJiance = indexMapper.findCurrentData(base.getFlag());
        if(indexMonitorVO == null){
            indexMonitorVO = new IndexMonitorVO();
        }
        indexMonitorVO.setJianceFlag(base.getFlag());
        if(custJiance != null){
            indexMonitorVO.setValue((custJiance.getJianceCount() == null ? "0" :
                    custJiance.getJianceCount().toString())+base.getUnit());
            indexMonitorVO.setDate(custJiance.getDataDate());
        }
        return indexMonitorVO;
    }
}
